package com.rongke.web.paymentrefund;

import com.rongke.web.heepay.util.HttpUtil;
import com.rongke.web.heepay.util.SmallTools;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


/**
 * Created by Administrator on 2017/7/21.
 * 退款接口
 */
public class Refund {
    public static void main(String[] args){
        String s = refund();
        System.out.println(s);
    }

    private static String refund() {
        //请求地址
        String url = "https://pay.heepay.com/API/Payment/PaymentRefund.aspx";
        //请求参数
        String agent_bill_id = "7551000001201707241100058782";    //商户系统内部的定单号
        String agent_id = "1664502";       //商户编号
        String notify_url = "http://www.hy.com";                //异步通知地址
        String version = "1";               //当前接口版本号
        String refund_details = "20170725170548,0.01,T20170725170548-3";   //退款数据
        String sign_type = "MD5";           //签名类型
        String key = "1C3A345D4F1E48B7A02B90AC";                  //签名密钥
        String sign = "";                   //签名结果

        //组织签名串
        StringBuilder sign_sb = new StringBuilder();
        sign_sb.append("agent_id")           .append("=").append(agent_id)      .append("&")
                .append("key")                .append("=").append(key)          .append("&")
                .append("notify_url")        .append("=").append(notify_url)   .append("&")
                .append("refund_details")   .append("=").append(refund_details).append("&")
                .append("version")           .append("=").append(version);
        System.out.println("签名参数："+sign_sb.toString().toLowerCase());
        sign = SmallTools.MD5en(sign_sb.toString().toLowerCase());
        System.out.println("签名结果："+sign);
        //请求参数
        StringBuilder requestParams = new StringBuilder();
        requestParams.append("agent_id") .append("=").append(agent_id)          .append("&")
                .append("notify_url")    .append("=").append(notify_url)        .append("&")
                .append("version")       .append("=").append(version)           .append("&")
                .append("refund_details").append("=").append(refund_details)   .append("&")
                .append("sign_type")     .append("=").append(sign_type)		  .append("&")
                .append("sign")           .append("=").append(sign);
        System.out.println("请求参数："+requestParams.toString());
        String ret = HttpUtil.sendPost(url,requestParams.toString());
        System.out.println("返回参数："+ret);
        //判断是否请求成功，成功解析xml，   失败直接返回失败原因
        if(!ret.contains("</agent_id>")){
            return ret ;
        }

        //验证返回的sign
        try {
            //解析XML形式的文本,得到document对象.
            org.dom4j.Document document = DocumentHelper.parseText(ret);
            //1.获取文档的根节点.
            Element rootElm = document.getRootElement();
            //2.取得某节点的单个子节点.
            Element m_agent_id = rootElm.element("agent_id");
            Element m_ret_code = rootElm.element("ret_code");
            Element m_ret_msg = rootElm.element("ret_msg");
            Element m_sign = rootElm.element("sign");

            //3.取得节点的文字
            String y_agent_id = m_agent_id.getText();
            String y_ret_code = m_ret_code.getText();
            String y_ret_msg = m_ret_msg.getText();
            String y_sign = m_sign.getText();

            System.out.println(y_agent_id);
            System.out.println(y_ret_code);
            System.out.println(y_ret_msg);
            System.out.println(y_sign);


            String sign1 = "agent_id="+y_agent_id+
                    "&key="+key+
                    "&ret_code="+y_ret_code+
                    "&ret_msg="+y_ret_msg;
            sign1 = sign1.toLowerCase();
            sign1 = SmallTools.MD5en(sign1);
            //判断验证签名是否成功
            if(y_sign.equals(sign1)){
                return ret;
            }else {
                return "签名验证失败";
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        return ret;
    }
}
